<?php
/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */

class ProductSearchComponent
{
	function ProductSearchComponent()
	{
	}

	function action(&$params, $action)
	{
		return $this->query($params);
	}

	function query(&$params) 
	{
		$handle = get_class($this);

		$productId = (int)$params['productId'];

		$subParams['handle'] = $handle;
		$subParams['languageId'] = (int)$params['languageId'];
		$subParams['desc_search'] = true;
		$subParams['max_rows'] = MAX_DISPLAY_SEARCH_RESULTS;
		if ($productId > 0) {
			$subParams['productId'] = $productId;
			$subParams['return_details'] = true;
		}
		else {
			$subParams['categoryId'] = (int)$params['categoryId'];
			$subParams['inc_subcat'] = true;
			$subParams['keywords'] = $params['keywords'];
			$subParams['offset'] = (int)$params['offset'];
			$subParams['return_page_info'] = true;
		}
		
		require_once('includes/components/ProductsSearchSubComponent.php');
		$subComponent = new ProductsSearchSubComponent();
		$return = $subComponent->query($subParams);
		$rows = (int)$return['ProductSearchComponentMetaData']['rows'];
		if ($rows == 0) {
			$subParams['keywords'] = null;
			$return = $subComponent->query($subParams);
			$return['ProductSearchComponentMetaData']['keywords_removed'] = true;
		}


		return $return;
	}
}
?>
